const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    goods_category_list: [
      {
        category_name: '加载中...'
      }
    ], //商品分类列表
    search_text: '', //搜索内容
    listClickFlag: 0,
    category_index: 0,
    is_load: 0,
    category_id: 0,
    category_template: 0,
    category_style: 0,
    category_is_img: 0,
    goods_list: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    app.getWindowSize(that);
    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    app.restStatus(that, 'listClickFlag');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadInfo(true);
    this.onShow();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    // 一级分类模板加载商品列表
    let category_template = that.data.category_template;
    let category_id = that.data.category_id;
    let page_index = that.data.page_index;

    if (category_template == 1) {
      let condition = {};
      let goods_list = that.data.goods_list;
      let length = goods_list.length;

      condition['ng.category_id'] = category_id;
      app.sendRequest({
        url: 'System.Goods.goodsList',
        data: {
          page_index: page_index,
          page_size: 10,
          condition: JSON.stringify(condition)
        },
        success: function(res) {
          let data = res.data;
          if (res.code == 0) {
            let data = res.data;

            if (app.checkArray(data.data)) {
              page_index++;
              let param = {};
              for (let index in data.data) {
                data.data[index].pic_cover_small = app.IMG(data.data[index].pic_cover_small);
                let goods_index = parseInt(length) + parseInt(index);

                let key = 'goods_list[' + goods_index + ']';
                console.log(key);
                param[key] = data.data[index];
              }
              let key = 'page_index';
              param[key] = page_index;
              that.setData(param);
            }
          }
        }
      })
    }
  },

  /**
   * 图片加载失败
   */
  imgError: function(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let goods_category_list = that.data.goods_category_list;
    let default_img = that.data.default_img;
    let hierarchy = dataset.hierarchy;
    let first_index = dataset.index;
    let second_index = dataset.key;
    let third_index = dataset.parm;
    let parm = {};
    let parm_key = '';
    let img = '';

    if (hierarchy == 2) {
      img = goods_category_list[first_index].child_list[second_index].category_pic;
      parm_key = 'goods_category_list[' + first_index + '].child_list[' + second_index + '].category_pic';
    } else if (hierarchy == 3) {
      img = goods_category_list[first_index].child_list[second_index].child_list[third_index].category_pic;
      parm_key = 'goods_category_list[' + first_index + '].child_list[' + second_index + '].child_list[' + third_index + '].category_pic';
    }

    if (default_img.is_use == 1) {
      let default_img = default_img.value.default_goods_img;

      if (img.indexOf(default_img) == -1) {
        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 商品分类广告图加载失败
   */
  categoryImgError: function(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let goods_category_list = that.data.goods_category_list;
    let category_index = dataset.index;
    let parm = {};
    let key = 'goods_category_list[' + category_index + '].category_pic';

    parm[key] = '';
    that.setData(parm);
  },

  /**
   * 加载页面信息
   */
  loadInfo: function(refreshFlag = false) {
    let that = this;
    // 分类展示类型
    app.sendRequest({
      url: 'System.Config.appletCategoryDisplay',
      data: {},
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          that.setData({
            category_template: data.template,
            category_style: data.style,
            category_is_img: data.is_img,
          })
          that.loadCategoryList();
        }
      }
    });
  },

  // 加载分类列表
  loadCategoryList: function() {
    let that = this;
    // 分类列表（树结构）
    app.sendRequest({
      url: 'System.Goods.goodsCategoryList',
      data: {},
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let goods_category_list = data;
          goods_category_list = app.checkEmpty(goods_category_list, []);
          that.loadGoodsList(goods_category_list[0].category_id);
          for (let index in goods_category_list) {

            //一级分类图片处理
            let first_img = goods_category_list[index].category_pic;
            goods_category_list[index].category_pic = app.IMG(first_img);

            //二级分类图片处理
            let second_list = goods_category_list[index].child_list;
            for (let key in second_list) {
              let second_img = second_list[key].category_pic;
              second_list[key].category_pic = app.IMG(second_img);

              //三级分类图片处理
              let third_list = second_list[key].child_list;
              for (let parm in third_list) {
                let third_img = third_list[parm].category_pic;
                third_list[parm].category_pic = app.IMG(third_img);
              }
              second_list[key].child_list = third_list;
            }
            goods_category_list[index].child_list = second_list;
          }
          that.setData({
            goods_category_list: goods_category_list,
            is_load: 1
          })
        }
      }
    });
  },

  // 加载商品列表
  loadGoodsList: function(category_id) {
    let that = this;
    let category_template = that.data.category_template;
    // 一级分类模板加载商品列表
    if (category_template == 1) {

      that.setData({
        category_id: category_id
      })
      wx.showNavigationBarLoading() //在标题栏中显示加载

      let condition = {};
      condition['ng.category_id'] = category_id;
      app.sendRequest({
        url: 'System.Goods.goodsList',
        data: {
          page_index: 1,
          page_size: 10,
          condition: JSON.stringify(condition)
        },
        success: function(res) {
          let data = res.data;
          if (res.code == 0) {
            let data = res.data;
            if (app.checkArray(data.data)) {
              for (let index in data.data) {
                data.data[index].pic_cover_small = app.IMG(data.data[index].pic_cover_small);
              }
              that.setData({
                goods_list: data.data,
                page_index: 2
              })
            }
          }
        },
        complete: function() {
          wx.hideNavigationBarLoading(); //完成停止加载
          wx.stopPullDownRefresh(); //停止下拉刷新
        }
      })
    }
  },

  /**
   * 选择分类
   */
  selectCategory: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    that.setData({
      category_id: id,
      category_index: index,
    })

    // 一级分类模板加载商品列表
    let category_template = that.data.category_template;
    if (category_template == 1) {
      let condition = {};
      condition['ng.category_id'] = id;
      app.sendRequest({
        url: 'System.Goods.goodsList',
        data: {
          page_index: 1,
          page_size: 10,
          condition: JSON.stringify(condition)
        },
        success: function(res) {
          let data = res.data;
          if (res.code == 0) {
            let data = res.data;

            if (app.checkArray(data.data)) {
              for (let index in data.data) {
                data.data[index].pic_cover_small = app.IMG(data.data[index].pic_cover_small);
              }
              // 切换分类后滚动条自动置顶
              that.setData({
                goods_list: []
              });
              that.setData({
                goods_list: data.data,
                page_index: 2
              })
            }
          }
        }
      })
    }
  },

  /**
   * 页面跳转
   */
  listClick: function(event) {
    let that = this;
    let url = event.currentTarget.dataset.url;
    let listClickFlag = that.data.listClickFlag;

    if (listClickFlag == 1) {
      return false;
    }
    app.clicked(that, 'listClickFlag');

    wx.navigateTo({
      url: '/pages' + url,
    })
  },

  /**
   * 输入框绑定事件
   */
  searchInput: function(event) {
    let search_text = event.detail.value;
    this.setData({
      search_text: search_text
    })
  }
})